<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务类型ID" prop="serviceTypeId">
        <el-input
          v-model="queryParams.serviceTypeId"
          placeholder="请输入服务类型ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医院科室ID" prop="hospitalId">
        <el-input
          v-model="queryParams.hospitalId"
          placeholder="请输入医院科室ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊人ID" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入就诊人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址ID" prop="addressId">
        <el-input
          v-model="queryParams.addressId"
          placeholder="请输入地址ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理时间" prop="clinicTime">
        <el-date-picker clearable
          v-model="queryParams.clinicTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择处理时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="报告类型" prop="reportType">
        <el-select v-model="queryParams.reportType" placeholder="请选择报告类型" clearable>
          <el-option
            v-for="dict in dict.type.accompany_report_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="药物类型" prop="medicineType">
        <el-select v-model="queryParams.medicineType" placeholder="请选择药物类型" clearable>
          <el-option
            v-for="dict in dict.type.accompany_medicine_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="药品名称" prop="medicineName">
        <el-input
          v-model="queryParams.medicineName"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['accompany:service:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['accompany:service:edit']"
        >电话已沟通</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handlePatientEscort"
          v-hasPermi="['accompany:service:edit']"
        >分配陪诊师</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['accompany:service:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['accompany:service:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="患者电话" align="center" prop="patientPhone" />
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="平台电话" align="center" prop="memberPhone" />
      <!--<el-table-column label="服务类型ID" align="center" prop="serviceTypeId" />-->
      <el-table-column label="服务名称" align="center" prop="typeName" />
      <el-table-column label="医院科室" align="center" prop="hospitalId">
        <template slot-scope="scope">
          <span>{{ scope.row.hospitalName}}/{{ scope.row.deptTypeName}}/{{ scope.row.deptName}}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="就诊人ID" align="center" prop="patientId" />-->
      <el-table-column label="邮寄地址" align="center" prop="addressId">
        <template slot-scope="scope">
          <span>{{ scope.row.addressName}}/{{ scope.row.address}}</span>
        </template>
      </el-table-column>
     <!--  <el-table-column label="订单id" align="center" prop="orderId" />-->
      <el-table-column label="注意事项" align="center" prop="announcements" />
      <el-table-column label="处理时间" align="center" prop="clinicTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.clinicTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务需求" align="center" prop="serviceRequirements" />
      <el-table-column label="报告类型" align="center" prop="reportType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_report_type" :value="scope.row.reportType"/>
        </template>
      </el-table-column>
      <el-table-column label="药物类型" align="center" prop="medicineType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_medicine_type" :value="scope.row.medicineType"/>
        </template>
      </el-table-column>
      <el-table-column label="药品名称" align="center" prop="medicineName" />
      <el-table-column label="订单状态" align="center" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_order_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="服务人员信息" align="center">
        <el-table-column label="姓名" align="center" prop="escortName" />
        <el-table-column label="电话" align="center" prop="escortPhone" />
      </el-table-column>
      <el-table-column label="是否沟通" align="center" prop="medicineName">
        <template slot-scope="scope">
          <span v-if="scope.row.isPhone=='0'" style="color: red;font-weight: bold;">否</span>
          <span v-else style="color: green;font-weight: bold;">已沟通</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['accompany:service:edit']"
          >电话已沟通</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accompany:service:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务类型ID" prop="serviceTypeId">
          <el-input v-model="form.serviceTypeId" placeholder="请输入服务类型ID" />
        </el-form-item>
        <el-form-item label="医院科室ID" prop="hospitalId">
          <el-input v-model="form.hospitalId" placeholder="请输入医院科室ID" />
        </el-form-item>
        <el-form-item label="就诊人ID" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入就诊人ID" />
        </el-form-item>
        <el-form-item label="地址ID" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入地址ID" />
        </el-form-item>
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="注意事项" prop="announcements">
          <el-input v-model="form.announcements" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理时间" prop="clinicTime">
          <el-date-picker clearable
            v-model="form.clinicTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务需求" prop="serviceRequirements">
          <el-input v-model="form.serviceRequirements" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="报告类型" prop="reportType">
          <el-select v-model="form.reportType" placeholder="请选择报告类型">
            <el-option
              v-for="dict in dict.type.accompany_report_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药物类型" prop="medicineType">
          <el-select v-model="form.medicineType" placeholder="请选择药物类型">
            <el-option
              v-for="dict in dict.type.accompany_medicine_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药品名称" prop="medicineName">
          <el-input v-model="form.medicineName" placeholder="请输入药品名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改陪诊师对话框 -->
    <el-dialog :title="title" :visible.sync="openEscort" width="500px" append-to-body>
      <el-form ref="formEscort" :model="form" :rules="escortRules" label-width="80px">
        <el-form-item label="报告类型" prop="escortId">
          <el-select v-model="form.escortId" placeholder="请选择陪诊师">
            <el-option
              v-for="dict in escortList"
              :key="dict.escortId"
              :label="dict.name"
              :value="dict.escortId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEscortForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listService, getService, delService, addService, updateService } from "@/api/accompany/service";
import { listAll } from "@/api/accompany/escort";

export default {
  name: "Service",
  dicts: ['accompany_report_type', 'accompany_medicine_type', 'accompany_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约管理表格数据
      serviceList: [],
      escortList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openEscort: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceTypeId: null,
        hospitalId: null,
        patientId: null,
        addressId: null,
        orderId: null,
        announcements: null,
        clinicTime: null,
        serviceRequirements: null,
        reportType: null,
        medicineType: null,
        medicineName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceTypeId: [
          { required: true, message: "服务类型ID不能为空", trigger: "blur" }
        ],
        hospitalId: [
          { required: true, message: "医院科室ID不能为空", trigger: "blur" }
        ],
        patientId: [
          { required: true, message: "就诊人ID不能为空", trigger: "blur" }
        ],
        addressId: [
          { required: true, message: "地址ID不能为空", trigger: "blur" }
        ],
      },
      escortRules: {
        escortId: [
          { required: true, message: "陪诊师不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
    listAll().then(res=>{
      console.log("陪诊师信息",res)
      this.escortList = res.data
    })
  },
  methods: {
    /** 查询预约管理列表 */
    getList() {
      this.loading = true;
      listService(this.queryParams).then(response => {
        this.serviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openEscort = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patientServiceId: null,
        serviceTypeId: null,
        hospitalId: null,
        patientId: null,
        addressId: null,
        orderId: null,
        announcements: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        clinicTime: null,
        serviceRequirements: null,
        reportType: null,
        medicineType: null,
        medicineName: null,
        escortId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.patientServiceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      /*this.reset();
      const patientServiceId = row.patientServiceId || this.ids
      getService(patientServiceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约管理";
      });*/
      const patientServiceIds = row.patientServiceId || this.ids;
      getService(patientServiceIds).then(response => {
        this.form = response.data;
        this.$modal.confirm('是否确认姓名为"' + this.form.patientName + '"的数据项已沟通完电话？').then(()=> {
          this.form.isPhone='1';
          return updateService(this.form)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("沟通完成");
        }).catch(() => {});

      });



    },
    // 分配陪诊师
    handlePatientEscort(row){
      const patientServiceIds = row.patientServiceId || this.ids;
      // 查一下是否存在陪诊师
      getService(patientServiceIds).then(response => {
        this.form = response.data;
        if(this.form.payStatus == 1){
          this.openEscort = true;
          this.title = "添加陪诊师管理";
        }else{
         this.$message.error("该订单不允许分配陪诊师");
        }
      });
    },
    submitEscortForm(){
      this.$refs["formEscort"].validate(valid => {
        if (valid) {
          console.log("通过验证...........",this.form)
          updateService(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openEscort = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.patientServiceId != null) {
            updateService(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addService(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patientServiceIds = row.patientServiceId || this.ids;
      this.$modal.confirm('是否确认删除预约管理编号为"' + patientServiceIds + '"的数据项？').then(function() {
        return delService(patientServiceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('accompany/service/export', {
        ...this.queryParams
      }, `service_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
